package com.wzh.glsc.ware.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wzh.common.utils.PageUtils;
import com.wzh.common.utils.Query;
import com.wzh.common.utils.R;
import com.wzh.glsc.ware.dao.WareInfoDao;
import com.wzh.glsc.ware.entity.WareInfoEntity;
import com.wzh.glsc.ware.feign.MemberServiceFeign;
import com.wzh.glsc.ware.service.WareInfoService;
import com.wzh.glsc.ware.vo.FareVo;
import com.wzh.glsc.ware.vo.MemberAddressVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.alibaba.fastjson.TypeReference;

import java.math.BigDecimal;
import java.util.Map;


@Service("wareInfoService")
public class WareInfoServiceImpl extends ServiceImpl<WareInfoDao, WareInfoEntity> implements WareInfoService {

    @Autowired
    private MemberServiceFeign memberServiceFeign;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<WareInfoEntity> page = this.page(
                new Query<WareInfoEntity>().getPage(params),
                new QueryWrapper<WareInfoEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPageByParams(Map<String, Object> params) {
        String key = (String) params.get("key");
        QueryWrapper<WareInfoEntity> wareInfoEntityQueryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(key)) {
            wareInfoEntityQueryWrapper.like("name", key)
                    .or().like("address", key)
                    .or().like("areacode", key);
        }
        IPage<WareInfoEntity> infoEntityIPage = this.page(new Query<WareInfoEntity>().getPage(params), wareInfoEntityQueryWrapper);
        return new PageUtils(infoEntityIPage);
    }

    @Override
    public FareVo addrFare( Long skuId) {
        R r = memberServiceFeign.info(skuId);
        MemberAddressVo memberReceiveAddress = r.getData("memberReceiveAddress", new TypeReference<MemberAddressVo>() {
        });
        FareVo fareVo = new FareVo();
        fareVo.setAddress(memberReceiveAddress);
        fareVo.setFare(new BigDecimal(memberReceiveAddress.getPhone().substring(memberReceiveAddress.getPhone().length() - 1)));
        return fareVo;
    }
}